package com.demo;

import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Service;

@Service
@RocketMQMessageListener(topic = "my-topic", consumerGroup = "my-group" )
public class MessageConsumer implements RocketMQListener<MessageExt> {

    @Override
    public void onMessage(MessageExt ext) {
        String message = new String(ext.getBody());
        System.out.println("接收到消息: " + message);
        try {
            // 模拟处理逻辑
            System.out.println("处理业务");
//            int i = 1 / 0;
            // 成功消费，自动提交 offset
        } catch (Exception e) {
            // 消费失败，抛出异常或记录日志，RocketMQ 会稍后重试
            System.err.println("消息消费失败，稍后重试");
            throw e; // 抛出异常表示消费失败，不会提交 offset
        }
    }
}
